package com.corex.vinayak;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

public class NotificationActivity extends Activity {

public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
    if(isRunning(getApplicationContext())) {
        finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately}
    }else{
        Intent intent = new Intent(this,VinayakActivity.class);
        startActivity(intent);
    }
}

    public boolean isRunning(Context ctx) {
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

        for (ActivityManager.RunningTaskInfo task : tasks) {
            Log.d("tag",task.baseActivity.getPackageName());
            if (ctx.getPackageName().equalsIgnoreCase(tasks.get(0).baseActivity.getPackageName()))
                return true;
        }

        return false;
    }

public static PendingIntent getDismissIntent(int notificationId, Context context) {
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(NOTIFICATION_ID, notificationId);
    PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    return dismissIntent;
}

}